<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('消息列表')" />
</head>
<body class="gray-bg">
    <div class="row m-b-lg m20">
        <div class="col-sm-12">
            <h1>消息中心</h1>
            <div class="tabs-container">
                <form id="msg-form" style="display: none">
                    <input id="type" name="type" type="hidden" th:value="${msgType}">
                </form>
                <ul class="nav nav-tabs" th:with="type=${@dict.getType('sys_msg_type')}">
                    <ul class="nav nav-tabs">
                        <li th:class="${msgType == 'all'? 'active':''}"><a data-toggle="tab" href="#tab-all" msgtype="all">全部消息</a>
                        </li>
                        <li th:each="dict : ${type}" th:class="${msgType == dict.getDictValue()? 'active':''}" ><a data-toggle="tab" th:msgtype="${dict.dictValue}" href="#tab-all" th:text="${dict.dictLabel}"></a>
                        </li>
                    </ul>
                    <div class="tab-content "  >
                        <div id="tab-all" class="tab-pane active">
                            <div class="panel-body">
                                <div class="row">
                                    <div class="col-sm-6">
                                        <div class="btn-group-sm" id="toolbar" role="group">
                                            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="sts:device:remove">
                                                <i class="fa fa-remove"></i> 删除
                                            </a>
                                            <a class="btn btn-warning" onclick="readFlag()" shiro:hasPermission="sts:device:export">
                                                <i class="fa fa-envelope-open"></i> 标记已读
                                            </a>
                                            <a class="btn btn-warning" onclick="readFlagAll()" shiro:hasPermission="sts:device:export">
                                                <i class="fa fa-envelope-open-o"></i> 全部已读
                                            </a>
                                        </div>
                                        <div class="col-sm-12 " style="height: 500px;">
                                            <table id="bootstrap-table"></table>
                                        </div>
                                    </div>
                                    <div class="col-sm-6" id="msgContent">

                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>

            </div>

        </div>

        </div>
    </div>

    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:msg:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:msg:remove')}]];
        var prefix = ctx + "system/msg";

        $(function() {
            var options = {
                url: prefix + "/list",
                removeUrl: prefix + "/remove",
                modalName: "消息",
                showSearch: false,
                showRefresh: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showPrint: false,
                sortName: "id",
                sortOrder: "desc",
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'id',
                        title: '主键',
                        visible: false
                    },

                {
                    field: 'title',
                    title: '消息标题'
                },
                    {
                        width: '100px',
                        field: 'status',
                        title: '发送时间',
                        formatter: function(value, row, index) {
                            if(value === "0"){
                                return "未读";
                            }else{
                                return "已读";
                            }

                        }
                    },
                    {
                        width: '150px',
                        field: 'sendTime',
                        title: '发送时间'
                    }],
                onClickRow:function (row, $element){
                    var ids = row.id;
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url: ctx + "system/msg/readFlag",
                        data: {
                            "ids": ids
                        },
                        error: function (request) {
                            $.modal.alertError("系统错误");
                        },
                        success: function (data) {

                        }
                    });
                    var msgTypeName = $("[msgtype="+row.type+"]").text();
                    $("#msgContent").html(' <h2>\n' +
                        '                                            '+msgTypeName+'<br/>\n' +
                        '                                        </h2>\n' +
                        '                                        <h3>\n' +
                        '                                            '+row.title+'<br/>\n' +
                        '                                        </h3>\n' +
                        '                                        <p>\n' +
                        '                                            '+row.content+'</p>\n' +
                        '                                        <small style="font-size: 6px;color: #6c757d!important">\n' +
                        '                                            消息发送于'+row.sendTime+'</small>');
                }
            };
            $.table.init(options);

            $("[msgtype]").click(function (){
                $("#msgContent").html("");
                var msgType = $(this).attr("msgtype");
                $("#type").val(msgType);
                $.table.search()
            });
        });

        function readFlag(){
            var columns = $('#bootstrap-table').bootstrapTable('getSelections');
            if(columns.length === 0){
                $.modal.msgError("请选择要标记的数据");return;
            }
            var ids = columns.map(it=>it.id).join(",");
            $.ajax({
                cache: true,
                type: "POST",
                url: ctx + "system/msg/readFlag",
                data: {
                    "ids": ids
                },
                async: false,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    if(data.code === 0){
                        $.table.search()
                        $.modal.msgSuccess("操作成功");
                    }else{
                        $.modal.msgError(data.msg);
                    }
                }
            });
        }

        function readFlagAll(){
            $.ajax({
                cache: true,
                type: "POST",
                url: ctx + "system/msg/readFlagAll",
                async: false,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    if(data.code === 0){
                        $.table.search()
                        $.modal.msgSuccess("操作成功");
                    }else{
                        $.modal.msgError(data.msg);
                    }
                }
            });
        }

    </script>
</body>
</html>